package team.seekers.hula.course.structure.section.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.seekers.hula.course.courseUser.dao.CourseUserDao;
import team.seekers.hula.course.courseUser.entity.dao.CourseUserDO;
import team.seekers.hula.course.courseUser.service.CourseUserService;
import team.seekers.hula.course.structure.section.dao.SectionProgressDao;
import team.seekers.hula.course.structure.section.entity.dao.SectionProgressDO;
import team.seekers.hula.course.structure.section.entity.dto.SectionProcessDTO;

import java.util.*;

@Service
public class SectionProgressService {

    @Autowired
    private SectionProgressDao sectionProgressDao;

    @Autowired
    private CourseUserService courseUserService;

    @Autowired
    private SectionService sectionService;

    public Integer getProgressBySectionId(String sectionContentId, String userId) {
        SectionProgressDO one = sectionProgressDao.lambdaQuery()
                .select(SectionProgressDO::getProgress)
                .eq(SectionProgressDO::getSectionId, sectionContentId)
                .eq(SectionProgressDO::getUserId, userId).one();
        if(one == null)
            return 0;
        else
            return one.getProgress();
    }

    public boolean saveOrUpdateProcess(SectionProcessDTO contentProcessDTO) {
        return sectionProgressDao.saveOrUpdate(SectionProgressDO.fromDTO(contentProcessDTO));
    }

    public boolean isSectionManager(String sectionCreatorId, String userId) {
        return userId.equals(sectionCreatorId);
    }

    public Integer getLastTimeBySectionContentId(String sectionContentId, String userId) {
        SectionProgressDO one = sectionProgressDao.lambdaQuery()
                .eq(SectionProgressDO::getSectionId, sectionContentId)
                .eq(SectionProgressDO::getUserId, userId).one();
        if(one == null)
            return 0;
        else
//            return one.getLastTime();
            return 0;
    }


    public void updateProgress(String sectionId, Integer progress, String userId) {
        SectionProgressDO one = sectionProgressDao.lambdaQuery().select(
                SectionProgressDO::getId,
                SectionProgressDO::getSectionId,
                SectionProgressDO::getProgress
        ).eq(SectionProgressDO::getSectionId, sectionId).eq(SectionProgressDO::getUserId, userId).one();
        if(one == null){
            SectionProgressDO sectionProgressDO = SectionProgressDO.builder()
                    .sectionId(sectionId).userId(userId)
                    .progress(progress)
                    .build();
            sectionProgressDao.save(sectionProgressDO);
        }else {
            if(!Objects.equals(progress, one.getProgress())){
                one.setProgress(progress);
                sectionProgressDao.updateById(one);
            }else {
                sectionProgressDao.updateById(one);
            }
        }

        // 更新总进度
        courseUserService.updateProgress(sectionId,progress,userId);
    }

    public void removeAllByUserIdAndCourseId(Long userId, List<Long> sectionIds) {

    }

    public Map<String, Integer> mapProgressBySectionIds(ArrayList<String> sectionIds,String id) {
        HashMap<String, Integer> map = new HashMap<>();
        if(sectionIds.size()>0){
            List<SectionProgressDO> sectionProgressDOList = sectionProgressDao.lambdaQuery()
                    .select(SectionProgressDO::getSectionId, SectionProgressDO::getProgress)
                    .eq(SectionProgressDO::getUserId, id)
                    .in(SectionProgressDO::getSectionId, sectionIds)
                    .list();

            for (SectionProgressDO sectionProgressDO : sectionProgressDOList) {
                map.put(sectionProgressDO.getSectionId(),sectionProgressDO.getProgress());
            }
        }
        return map;
    }

    public Integer countProgressUpdate(String courseId,String userId) {
        List<String> sectionIds = sectionService.getIdsByCourseId(courseId);
        List<SectionProgressDO> sectionProgressDOList = sectionProgressDao.lambdaQuery()
                .select(SectionProgressDO::getSectionId, SectionProgressDO::getProgress)
                .eq(SectionProgressDO::getUserId, userId)
                .in(SectionProgressDO::getSectionId, sectionIds)
                .list();
        Integer progressAll = 0;
        for (SectionProgressDO sectionProgressDO : sectionProgressDOList) {
            progressAll = progressAll + sectionProgressDO.getProgress();
        }

        return Math.max(progressAll/sectionIds.size(),1);
    }
}
